﻿Imports System.Data.OleDb
Imports System.Net.Mail
Public Class ééaandeelhouder
    Inherits System.Web.UI.Page
    Dim strGekozenVergadering As String
    Dim strcode As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            btnVersturen.Enabled = False
            Dim cnndb3 As New OleDbConnection
            Dim cmdCommando As New OleDbCommand
            Dim dtrDataReader As OleDbDataReader
            Dim strsqlEmailadress As String
            Dim strsqlVergaderingen As String
            strsqlEmailadress = "SELECT [E-mail adress],[Code] FROM [tblemailadres]"
            cnndb3.ConnectionString = My.Settings.csAandeelhouders
            cmdCommando.Connection = cnndb3
            cmdCommando.CommandText = strsqlEmailadress


            cnndb3.Open()
            dtrDataReader = cmdCommando.ExecuteReader
            Do While dtrDataReader.Read()
                dpdEmailadres.Items.Add(dtrDataReader.Item("E-mail adress"))
                strcode = dtrDataReader.Item("Code")
            Loop
            cnndb3.Close()
            If IsNothing(dtrDataReader) Then
                dtrDataReader.Close()
            End If
            strsqlVergaderingen = "SELECT [Vergadering] FROM [tblVergadering]"
            cmdCommando.CommandText = strsqlVergaderingen
            cnndb3.Open()
            dtrDataReader = cmdCommando.ExecuteReader
            Do While dtrDataReader.Read()
                dpdVergadering.Items.Add(dtrDataReader.Item("Vergadering"))
            Loop
            cnndb3.Close()

        End If


    End Sub

    Protected Sub btnVersturen_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVersturen.Click
        'declareer variabele
        myReplace()
        Dim objMail As New MailMessage()
        Dim objMijnAdres As New MailAddress("Proventi@hotmail.be", "Proventi")
        Dim objSmtp As New SmtpClient(My.Settings.SMTPServer)
        Dim strAan As String
        strAan = dpdEmailadres.Text

        If strAan IsNot "" Then
            'stel de verzender en ontvanger in 
            objMail.From = objMijnAdres
            objMail.To.Add(strAan)
            'stel het subject en de tekst in 
            objMail.Subject = txtOnderwerp.Text
            objMail.Body = mailContent.InnerText
            objMail.IsBodyHtml = True
            'verzend het bericht 
            objSmtp.Send(objMail)
            lblMailVerstuurd.Text = "De mail is verstuurd"
        End If

    End Sub

    Protected Sub btnMenu_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMenu.Click
        Server.Transfer("default.aspx")
    End Sub


    Public Sub btnVoorbeeld_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVoorbeeld.Click
        strGekozenVergadering = dpdVergadering.Text

        myReplace()



    End Sub
    Public Function myReplace() As Boolean
        Static strDatum1 As String = "[$date1$]"
        Static strDatum2 As String = "[$date2$]"
        Static strPlaats As String = "[$plaats$]"
        strGekozenVergadering = dpdVergadering.Text
        Dim strCode As String
        Dim strDatum1Db As String
        Dim strDatum2Db As String
        Dim strPlaatsDb As String
        Dim cnndb3 As New OleDbConnection
        Dim cmdCommando As New OleDbCommand
        Dim dtrDataReader2 As OleDbDataReader
        Dim strsqlEmailadress As String


        strsqlEmailadress = "SELECT [Datum], [Start datum], [Plaats] FROM [tblVergadering] WHERE [Vergadering] ='" & strGekozenVergadering & "'"
        cnndb3.ConnectionString = My.Settings.csAandeelhouders
        cmdCommando.Connection = cnndb3
        cmdCommando.CommandText = strsqlEmailadress


        cnndb3.Open()
        dtrDataReader2 = cmdCommando.ExecuteReader
        Do While dtrDataReader2.Read()
            strDatum1Db = dtrDataReader2.Item("Datum")
            strDatum2Db = dtrDataReader2.Item("Start datum")
            strPlaatsDb = dtrDataReader2.Item("Plaats")

        Loop
        cnndb3.Close()
        If IsNothing(dtrDataReader2) Then
            dtrDataReader2.Close()
        End If
        mailContent.InnerHtml = mailContent.InnerHtml.Replace(strDatum1, strDatum1Db)
        mailContent.InnerHtml = mailContent.InnerHtml.Replace(strDatum2, strDatum2Db)
        mailContent.InnerHtml = mailContent.InnerHtml.Replace(strPlaats, strPlaatsDb)
        mailContent.InnerHtml = mailContent.InnerHtml.Replace(".uniqu.", "<a href=AanofAfwezig.aspx?id=" & strCode & ">Klik hier voor aanwezig of afwezig</a>")


        strDatum1 = strDatum1Db
        strDatum2 = strDatum2Db
        strPlaats = strPlaatsDb


        btnVersturen.Enabled = True
        Return True

    End Function
End Class